home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5044 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Got Questions About Multi-Dimension Arrays (After Reading FAQ)
  5. Date: 10 Feb 1996 23:07:47 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb10160747@qcd.lanl.gov>
  8. References: <4ep87b$o60@alcor.usc.edu> <harmon.823155385@pegasus.montclair.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: harmon@pegasus.montclair.edu's message of 1 Feb 1996 01:18:41 -0500
  13.  
  14. In article <harmon.823155385@pegasus.montclair.edu>
  15. harmon@pegasus.montclair.edu (Derek Harmon) writes: 
  16. <snip>
  17.    > int m[2][2] = {1, 2, 3, 4};
  18.  
  19.        I always m[2][2] = { {1, 2}, {3, 4} }; else it gets very confusing.
  20.    >
  21.    > myfunc(m,2,2);
  22.  
  23.        Since the array name m devolves into a pointer to an int which is
  24.    the first element of the array, *array = 1 in the function when called
  25.    in this way.
  26.  
  27. This is incorrect. After `int m[2][2]', m decays to a pointer to the
  28. first _subarray of int_ and has the type `int (*)[2]'. It does not become an
  29. int * ... and with myfunc's prototype in scope, a compiler must give
  30. an error because there is no auto conversion from int (*)[2] to int*.
  31.  
  32.    > So why can't C convert from pointer to integer to a multidemsional
  33.    > array. The reason I want to do this is because my function needs to
  34.    > take in an array of any width and height. Thank you for your time,
  35.  
  36.        A dynamic array huh?  You need to think in pointers (note the
  37.    plurality) Arrays are really pointers, the more dimensions, the more
  38.    pointers you need.
  39.  
  40. The FAQ has a much better discussion of this. 
  41.  
  42.  
  43.        Try,
  44.  
  45.    : void yourfunc(int *array[], int rows, int cols)
  46.  
  47.        The (int *)array can be indexed with one [] index in your function,
  48.    and then you can use the other [] to index rows (or is it columns?) :)
  49.    What it really comes down to is int **array, a double pointer!  Think
  50.    about it.  C is full of hobgoblins, most are more gruesome though.
  51.  
  52. Remember, a pointer to a pointer is not a pointer to an array. Hence,
  53. with 'int m[2][2];', yourfunc(m,2,2) is equally wrong: in fact more
  54. wrong if that makes sense.
  55.  
  56. Cheers
  57. Tanmoy--
  58. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  59. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  60. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  61. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  62. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  63. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  64.